import requests

import os
import re

from Crypto.Cipher import AES

_dir = os.path.dirname(os.path.abspath(__file__))

def get_ts():
    video_url = 'https://www.52netflix.com'
    d_m3u8 = requests.get(url=f'{video_url}/videos/202411/10/6730159f1367414ba205ef61/2230g7/index.m3u8?counts=18&timestamp=1739177290000&key=b2c650b7988a770cde2c9750c22d8155').content.decode('utf-8')
    ts_list = re.findall(r'(.*?\.ts)', d_m3u8)     # 正则提取出列表
    key_code = ''
    if 'URI' in d_m3u8:
        key_url = re.findall(r'URI="(.*?)"',d_m3u8)[0] #正则提取出秘钥
    return ts_list, key_code

ts_list,key_code = get_ts()

# print("索引文件: ", ts_list)
# print("密钥：", key_code)

name = ts_list[0].split("/")[-1]
print("索引名称：", name)
with open(os.path.join(_dir, name),'wb') as f:
    f.write(requests.get(url= ts_list[0] ).content)

# if key_code != '':
#     for i in ts_list:
#         name = i.split("/")[-1]
#         with open(os.path.join(_dir, name),'wb') as f:
#             f.write(requests.get(url= i ).content)
#         aes = AES.new(key=bytes(key_code.encode()),IV=b'0000000000000000',mode=AES.MODE_CBC)
#         with open(os.path.join(_dir, i + 'new.mp4'),'wb') as fp:
#             with open(os.path.join(_dir, i),'rb') as f:
#                 fp.write(aes.decrypt(f.read()))
#     print('解密成功')
